import HDataTable from "@/components/ant-ext-table/HDataTable";
import {Form, Table} from "antd";
import React from "react";
import {Dispatch} from "redux";
import {HTableProps} from "@/components/ant-ext-table/HTable";
import {useDispatch} from "react-redux";
import EntityPaymentTrade from "@/api/entity-payment-trade";

function getColumns(dispatch: Dispatch): HTableProps<any>["columns"] {
    return [
        {
            title: "日期",
            dataIndex: 'date',
            width: '300px',
            fixed: 'left',
        }, {
            title: "支付金额",
            dataIndex: 'amount',
        }, {
            title: "分润金额",
            dataIndex: 'shareProfit',
        }, {
            title: "最终收益",
            render: (records) => {
                return <>{(records.amount - records.shareProfit).toFixed(3)} </>
            }
        }
    ]
}

export default function StatisticsList() {
    const [form] = Form.useForm()
    const dispatch = useDispatch()

    return <>
        <HDataTable
            queryForm={{
                form: form,
                item: [
                    {name: "dates", label: "时间", type: "dateRangePicker", width: "150px"},
                ]
            }}
            pagination={false}
            columns={getColumns(dispatch)} selectMethod={EntityPaymentTrade.selectStatisticsList}
            rowKey={EntityPaymentTrade.RowKey}
            summary={(data) => {
                let amount = 0;
                let shareProfit = 0
                for (let i in data) {
                    let item = data[i]
                    amount += item.amount
                    shareProfit += item.shareProfit
                }
                return <Table.Summary fixed>
                    <Table.Summary.Row style={{textAlign: "center"}}>
                        <Table.Summary.Cell index={0} colSpan={6}>汇总</Table.Summary.Cell>
                    </Table.Summary.Row>
                    <Table.Summary.Row>
                        <Table.Summary.Cell index={0} colSpan={3}></Table.Summary.Cell>
                        <Table.Summary.Cell index={3}>总支付金额</Table.Summary.Cell>
                        <Table.Summary.Cell index={4}>总分润</Table.Summary.Cell>
                        <Table.Summary.Cell index={5}>总收益</Table.Summary.Cell>
                    </Table.Summary.Row>
                    <Table.Summary.Row>
                        <Table.Summary.Cell index={0} colSpan={3}></Table.Summary.Cell>
                        <Table.Summary.Cell index={3}>{amount.toFixed(3)}</Table.Summary.Cell>
                        <Table.Summary.Cell index={4}>{shareProfit.toFixed(3)}</Table.Summary.Cell>
                        <Table.Summary.Cell index={5}>{(amount - shareProfit).toFixed(3)}</Table.Summary.Cell>
                    </Table.Summary.Row>
                </Table.Summary>
            }}
        />
    </>
}